package com.yanqu.road.server.channelserver.cmd.activity.tonbing;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.rank.UnionRankMsgModel;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.activity.ChannelActivityInfoMgr;
import com.yanqu.road.server.channelserver.manager.activity.ChannelActivityUnionMgr;
import com.yanqu.road.server.cmd.CrossChannelCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Objects;

@Cmd(code = ChannelProtocol.N_UNION_GET_MAX_ABILITY_UNION_RANK_LIST, desc = "")
public class ChannelUnionMaxAbilityRankListCmd extends CrossChannelCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RankProto.GetActivityUnionRankListRespMsg reqMsg = RankProto.GetActivityUnionRankListRespMsg.parseFrom(packet.getBytes());

        //是不是渠道内的排名
        boolean isChannelRank = false;
        ActivityInfo activityInfo = ChannelActivityInfoMgr.getActivityInfo(reqMsg.getActivityId());
        if(activityInfo.getType() == eActivityType.ChannelTonBingActivity.getValue()){
            isChannelRank = true;
        }

        UnionRankMsgModel model = ChannelActivityUnionMgr.getUnionRankMsgModel(reqMsg.getActivityId());

        RankProto.GetActivityUnionRankListRespMsg.Builder respMsg = reqMsg.toBuilder();
        if(model != null){
            RankProto.UnionRankListMsg rankListMsg = model.getRankListMsg();
            respMsg.setRank(rankListMsg);
            int rank = 0;
            int myChannelId = ChannelActivityUnionMgr.getUnionChannel(reqMsg.getActivityId(),reqMsg.getUnionId());
            for (RankProto.UnionRankTempMsg rankTempMsg : rankListMsg.getRankListList()) {
                if(!isChannelRank){
                    rank++;
                }else{
                    int channelId = ChannelActivityUnionMgr.getUnionChannel(reqMsg.getActivityId(),rankTempMsg.getUnionUid());
                    if(channelId == myChannelId){
                        rank++;
                    }
                }
                if (Objects.equals(rankTempMsg.getUnionUid(), reqMsg.getUnionId())) {
                    respMsg.setMyValue(rankTempMsg.getValue());
                    respMsg.setMyRank(rank);
                    break;
                }
            }
        }
        ChannelMessageHelper.sendGamePacket(Protocol.U_CHANNEL_UNION_MAX_ABILITY_RANK_LIST, respMsg,respMsg.getChannelId(),respMsg.getServerId(),packet.getPlayerId());

        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData() {

    }
}
